#define _CRT_SECURE_NO_WARNINGS 1


#include<vector>
#include<string>
#include<map>
#include<iostream>

using namespace std;

bool Func(map<string, int>& hash, map<string, int>& hash_words)
{
	auto it1 = hash.begin();
	auto it2 = hash_words.begin();
	while (it1 != hash.end())
	{
		if (it1->first != it2->first || it1->second != it2->second)
		{
			return false;
		}
		it1++;
		it2++;
	}
	return true;
}

vector<int> findSubstring(string s, vector<string>& words)
{
	map<string, int> hash_word;
	for (auto& e : words)
	{
		hash_word[e]++;
	}
	int size = s.size();
	int len = words[0].size();
	int left = 0, right = 0, count = 0;
	vector<int> v;
	for (int i = 0; i < len; i++)
	{
		map<string, int> hash;
		left = right = i;
		count = 0;
		while (right + len < size)
		{
			string str(s.begin() + right, s.begin() + right + len);
			hash[str]++;
			count++;
			while (count == words.size())
			{
				if (Func(hash, hash_word))
				{
					v.push_back(left);
				}
				string str1(s.begin() + left, s.begin() + left + len);
				hash[str1]--;
				count--;
				if (hash[str1] == 0)
				{
					hash.erase(str1);
				}
				left += len;
			}
			right += len;
		}
	}
	return v;
}

int main()
{
	string s = "barfoothefoobarman";
	vector<string> v = { "foo", "bar" };
	vector<int> ret = findSubstring(s, v);
	for (auto& e : ret)
	{
		cout << e << ' ';
	}
	cout << endl;

	return 0;
}